package com.ropeok.dao.impl;

import com.ropeok.dao.UserAndRoleDao;
import com.ropeok.entity.Role;
import com.ropeok.entity.User;
import com.ropeok.util.c3p0Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserAndRoleDaoImpl implements UserAndRoleDao {
    Connection connection= c3p0Util.getConnection();
    @Override
    public void saveUR(User user, Role role) {
        try{
            PreparedStatement preparedStatement=connection.prepareStatement("insert into t_user values (?,?)");
            preparedStatement.setInt(1,user.getUserid());
            preparedStatement.setInt(2,role.getRoleid());
            preparedStatement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public void DelURUser(Integer id) {
        try {
            PreparedStatement pstm1=connection.prepareStatement("delete from user_role where user_id=? ");
            pstm1.setInt(1,id);
            pstm1.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    @Override
    public void DelURRole(Integer id){
        try {
            PreparedStatement pstm1=connection.prepareStatement("delete from user_role where role_id=? ");
            pstm1.setInt(1,id);
            pstm1.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
